In [5]:
import cv2
import numpy as np
from matplotlib import pyplot as plt
from csv import reader
import pandas as pd
from tensorflow import keras
from tensorflow.keras import applications
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras import optimizers
from tensorflow.keras.models import Sequential, Model 
from tensorflow.keras.layers import *
from tensorflow.keras.callbacks import ModelCheckpoint, LearningRateScheduler, TensorBoard, EarlyStopping
import os
from sklearn.model_selection import train_test_split
import tensorflow.keras.metrics as km
from sklearn.metrics import accuracy_score
from sklearn.metrics import precision_score
from sklearn.metrics import recall_score
from sklearn.metrics import f1_score
from sklearn.metrics import cohen_kappa_score
from sklearn.metrics import roc_auc_score
from sklearn.metrics import multilabel_confusion_matrix
from sklearn.metrics import classification_report
In [3]:
#build dataset for ML model training

X = []
Y = []

color_line = (255, 255, 255)
thickness = 10
img_height , img_width = 400, 100

previous_group = "empty"
image=np.zeros((1000,4000,3),np.uint8)
previous_point = (0,0)
frames_list = []

with open('ftir_metadata.csv', 'r') as read_obj_classes:
    csv_reader_classes = reader(read_obj_classes)
    header = next(csv_reader_classes)
    classes = []
    for row in csv_reader_classes:
      if row[0] not in classes:
        classes.append(row[0])

with open('ftir_library.csv', 'r') as read_obj:
    csv_reader = reader(read_obj)
    header = next(csv_reader)
    sortedlist = sorted(csv_reader, key=lambda row: int(row[3]))
    df=pd.read_csv("ftir_metadata.csv")

    # Iterate over each row in the csv using reader object
    for row in sortedlist:
        group = row[3]
        point = (int(float(row[0])), int(float(row[1])*1000))

        if previous_group == "empty":
            image=np.zeros((1000,4000,3),np.uint8)
            frames_list = []
            previous_group = group            
        elif (group != previous_group):
            y = [0]*len(classes)     
            val = df.iloc[int(previous_group)-1]['spectrum_identity']
            index = classes.index(val)
            y[index] = 1
            Y.append(y)

            print(previous_group)
            print(classes[index])
            plt.figure(figsize=(20, 20))
            plt.imshow(image, origin='lower')
            plt.show()

            image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
            image = cv2.resize(image, (img_height, img_width))
            frames_list.append(image)
            X.append(frames_list)

            frames_list = []
            image=np.zeros((1000,4000,3),np.uint8)

            previous_group = group
        else:           
            image = cv2.line(image, previous_point, point, color_line, thickness)  

        previous_point = point

    y = [0]*len(classes)     
    val = df.iloc[int(previous_group)-1]['spectrum_identity']
    index = classes.index(val)
    y[index] = 1
    Y.append(y)  

    print(previous_group)
    print(classes[index])
    plt.figure(figsize=(20, 20))
    plt.imshow(image, origin='lower')
    plt.show()

    image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    image = cv2.resize(image, (img_height, img_width))        

    frames_list.append(image)
    X.append(frames_list)
1
HDPE
2
HDPE
3
HDPE
4
HDPE
5
HDPE
6
HDPE
7
HDPE
8
HDPE
9
HDPE
10
HDPE
11
HDPE
12
HDPE
13
HDPE
14
HDPE
15
HDPE
16
HDPE
17
HDPE
18
HDPE
19
HDPE
20
HDPE
21
HDPE
22
HDPE
23
HDPE
24
HDPE
25
HDPE
26
HDPE
27
HDPE
28
HDPE
29
HDPE
30
HDPE
31
HDPE
32
HDPE
33
HDPE
34
HDPE
35
HDPE
36
HDPE
37
HDPE
38
HDPE
39
HDPE
40
HDPE
41
HDPE
42
HDPE
43
HDPE
44
HDPE
45
HDPE
46
HDPE
47
HDPE
48
HDPE
49
HDPE
50
HDPE
51
HDPE
52
HDPE
53
HDPE
54
HDPE
55
HDPE
56
HDPE
57
HDPE
58
HDPE
59
HDPE
60
HDPE
61
HDPE
62
HDPE
63
HDPE
64
HDPE
65
HDPE
66
HDPE
67
HDPE
68
HDPE
69
HDPE
70
HDPE
71
HDPE
72
HDPE
73
HDPE
74
HDPE
75
HDPE
76
HDPE
77
HDPE
78
HDPE
79
HDPE
80
HDPE
81
HDPE
82
HDPE
83
HDPE
84
HDPE
85
HDPE
86
HDPE
87
HDPE
88
HDPE
89
HDPE
90
HDPE
91
HDPE
92
HDPE
93
HDPE
94
HDPE
95
HDPE
96
HDPE
97
HDPE
98
HDPE
99
HDPE
100
HDPE
101
HDPE
102
HDPE
103
HDPE
104
HDPE
105
HDPE
106
HDPE
107
HDPE
108
HDPE
109
HDPE
110
HDPE
111
HDPE
112
HDPE
113
HDPE
114
HDPE
115
HDPE
116
HDPE
117
HDPE
118
HDPE
119
HDPE
120
HDPE
121
HDPE
122
HDPE
123
HDPE
124
HDPE
125
HDPE
126
HDPE
127
HDPE
128
HDPE
129
HDPE
130
HDPE
131
HDPE
132
HDPE
133
HDPE
134
HDPE
135
HDPE
136
HDPE
137
HDPE
138
PET
139
PET
140
PET
141
PET
142
PET
143
PET
144
PET
145
PET
146
PET
147
PET
148
PET
149
PET
150
PET
151
PET
152
PET
153
PET
154
PET
155
PET
156
PET
157
PET
158
PET
159
PET
160
PET
161
PET
162
PET
163
PET
164
PET
165
PET
166
PET
167
PET
168
PET
169
PET
170
PET
171
PET
172
PET
173
PET
174
PET
175
PET
176
PET
177
PET
178
PET
179
PET
180
PET
181
PET
182
PET
183
PET
184
PET
185
PET
186
PET
187
PET
188
PET
189
PET
190
PET
191
PET
192
PET
193
PET
194
PET
195
PET
196
PET
197
PET
198
PET
199
PET
200
PET
201
PET
202
PET
203
PET
204
PET
205
PET
206
PET
207
PET
208
PET
209
PET
210
PET
211
PET
212
PET
213
PET
214
PET
215
PET
216
PET
217
PET
218
PET
219
PET
220
PET
221
PET
222
PET
223
PET
224
PET
225
PET
226
PET
227
PET
228
PET
229
PET
230
PET
231
PET
232
PET
233
PET
234
PET
235
PET
236
PET
237
PET
238
PET
239
PET
240
PET
241
PET
242
PET
243
PET
244
PET
245
PET
246
PET
247
PET
248
PET
249
PET
250
PET
251
PET
252
PET
253
PET
254
PET
255
PET
256
PET
257
PET
258
PET
259
PET
260
PET
261
PET
262
PET
263
PET
264
PET
265
PET
266
PET
267
PET
268
PET
269
PET
270
PET
271
PET
272
PET
273
1 2 polybutadiene
274
acrylonitrile butadiene styrene
275
acrylonitrile butadiene styrene
276
fibre acetate
277
acrylonitrile butadiene
278
algae desmarestia viridis
279
algae fucus serratus
280
algae laminaria digita and hyperborea
281
algae laminaria sacharina
282
alginic acid  sodium salt
283
fur alpaca
284
polyethylene wax oxidized
285
polyethylene wax
286
polyethylene wax
287
fur angora rabbit
288
broodcomb
289
amber
290
polyester epoxide
291
epoxide resin
292
polyester
293
polyester epoxide
294
polyhydroxybutyric acid
295
butyl methacrylate isobutyl methacrolate
296
fur camel
297
wool cashmere crossbred
298
wool cashmere kasakhstan
299
wool cashmere mongolia
300
fur cat european shorthair
301
cellulose
302
cellulose acetate
303
cellulose acetate butyrate
304
cellulose
305
cellulose triacetate
306
cellulose propionate
307
chitin crangon antonia
308
chitin from crustacean shells
309
chitin cancer pagurus
310
fibre cocoanut
311
copolyamide
312
copolyamide
313
copolyester
314
copolyester
315
copolyester
316
fibre cotton combers
317
fibre cotton US pima
318
fibre cotton uzbekistan
319
fur cow
320
crangon  chitin exuvie
321
polyethylene wax
322
polyethylene wax
323
polyethylene wax oxidized
324
sealing ring EPDM
325
sealing ring EPDM
326
sealing ring Gardena 1124 large
327
sealing ring Gardena 1124 small
328
sealing ring Gardena 2824 large
329
sealing ring Gardena 2824 small
330
sealing ring Gardena 2824 medium
331
fur dog
332
honeycomb
333
polystyrene expanded
334
copolyester
335
copolyester
336
silicone rubber
337
polystyrene expanded
338
ethyl cellulose
339
ethylene acrylic acid
340
ethylene ethyl acrylate
341
ethylene methacrylic acid
342
ethylene propylene
343
ethylene vinyl acetate
344
ethylene vinyl acetate
345
ethylene vinyl acetate
346
ethylene vinyl alcohol
347
ethylene vinyl alcohol
348
ethylene vinyl alcohol
349
polyethylene high density
350
fibre urtica dioica L conar fibra
351
fibre flax
352
honeycomb freshly removed with nectar
353
honeycomb freshly removed
354
fibre grass
355
polyethylene high density
356
polyethylene high density
357
polyethylene high density
358
fibre hemp fine
359
fibre hemp rough
360
alkyd varnish
361
alkyd varnish
362
wood beech
363
wood pine
364
wood glue
365
wood mahagoni
366
broodcomb once brooded
367
polytehylene terephthalate
368
polytehylene terephthalate
369
hydroxyethyl cellulose
370
hydroxypropyl cellulose
371
hydroxypropyl methyl cellulose
372
fibre indian raw cotton
373
fibre jute
374
copolyamide
375
fibre kapok
376
polyvinylchloride
377
polypropylene
378
polyethylene foamed
379
silica gel lab
380
polyurethane acrylic resin
381
cellulose wipe
382
lahmian medium acrylic paint
383
fur lama
384
polyethylene low density
385
polyethylene low density
386
polyethylene low density
387
polyethylene low density
388
polyethylene low density
389
fibre linen
390
fibre linen
391
polyethylene low density
392
polyethylene high density
393
alkyd varnish
394
merino scoured wool made rough
395
alkyd varnish
396
methyl cellulose
397
methyl vinyl ether maleic acid
398
methyl vinyl ether maleic anhydride
399
honeycomb middle wall
400
fur mohair angora goat
401
polyimide
402
aramid
403
aramid
404
polytehylene terephthalate
405
polyamide
406
polycarbonate
407
polyester
408
polyvinylchloride
409
polyester
410
polyester
411
polypropylene
412
fibre mulberry silk
413
nitrile rubber
414
nylon 6 (3)  T
415
nylon 6
416
nylon 6 6
417
nylon 6 9
418
nylon 6 12
419
nylon 11
420
nylon 12
421
n vinylpyrrolidone vinyl acetate
422
honeycomb top bar
423
fibre polyamide 6 (not)stretched
424
fibre polyamide 6
425
polyamide 6
426
polyamide 6
427
polyamide 6
428
fibre polyamide 6 P6.6
429
polymaide 66
430
polybuthylene terephthalate
431
polybuthylene terephthalate
432
polycarbonate
433
polycarbonate
434
polyesterurethane
435
copolyester
436
polyester
437
polytehylene terephthalate
438
polytehylene terephthalate amorphous
439
polytehylene terephthalate
440
phenoxy resin
441
polylactic acid
442
polylactic acid
443
poly(1 butene) isotactic
444
poly(2 4 6 tribromostyrene)
445
poly(2 6 dimethyl p phenylene oxide)
446
poly(2 hydroxyethyl methacrylate)
447
poly(4 4' dipropoxy 2 2' diphenyl propane fumarate)
448
poly(4 methyl 1 pentene)
449
poly(acrylic acid)
450
poly(butylene terephthalate)
451
poly(diallyl isophthalate)
452
poly(ethylene glycol)
453
poly(ethylene oxide)
454
poly(ethylene terephthalate)
455
poly(ethyl methacrylate)
456
poly(isobutyl methacrylate)
457
poly(methyl methacrylate)
458
poly(n butyl methacrylate)
459
poly(phenylene sulfide)
460
poly(p phenylene ether sulphone)
461
poly(tetrafluoroethylene)
462
poly(vinyl acetate)
463
poly(vinyl alcohol)
464
poly(vinyl alcohol)
465
poly(vinyl butyral)
466
poly(vinyl chloride)
467
poly(vinyl chloride) carboxylated
468
poly(vinyl formal)
469
poly(vinylidene fluoride)
470
poly(vinyl stearate)
471
polyacetal
472
polyacrylamide
473
polyacrylamide carboxyl modified
474
fibre polyactide
475
polyamide resin
476
polycaprolactone
477
polycaprolactone
478
polycarbonate
479
polycarbonate
480
polychloroprene
481
fibre polyester
482
fibre polyester
483
fibre polyetheretherketone
484
polyethylene low density
485
polyethylene chlorinated
486
polyethylene chlorinated
487
polyethylene chlorinated
488
polyethylene chlorosulfonated
489
polyethylene high density
490
polyethylene low density
491
polyethylene oxidized
492
polyethylene terephtalate
493
polyisoprene chlorinated
494
fibre polypropylene
495
fibre polypropylene dyed
496
polypropylene isotactic
497
polypropylene
498
polystyrene
499
polysulfone
500
polytetrafluoroethylene
501
polyurethane
502
polyurethane
503
polyurethane
504
polyurethane
505
fibre polyvinyl alcohol
506
polyvinyl alcohol
507
fibre polyvinylidene fluoride
508
polyvinylpyrrolidone
509
polyoxymethylene
510
fibre poplar down
511
fibre poplar down
512
polypropylene
513
polypropylene
514
polypropylene
515
polypropylene
516
polyphenylsulfone
517
polypropylene
518
polystyrene
519
polystyrene
520
polystyrene
521
polyesterurethane
522
polyesterurethane
523
polyetherurethane
524
polyetherurethane
525
polyurethane
526
polyetherester
527
polyvinylchloride with plasticizer
528
quartz sand lab
529
quartz sand beach
530
wool raw cashmere afghanistan
531
wool raw cashmere mongolia
532
fur red deer
533
fibre roasted flax
534
polyethylene low density linear
535
styrene acrylonitrile
536
polyethylene low density
537
polyethylene low density
538
polyester
539
polyester
540
polyethylene high density
541
polyethylene high density
542
polyethylene high density
543
copolyamide
544
copolyamide
545
polyamide
546
polyurethane
547
polyurethane
548
polyurethane
549
windscreen wiper rubber
550
black broodcomb
551
scoured wool  not made rough
552
silicone seal reactor
553
fibre silk slubbing
554
resin dispersion
555
styrene acrylonitrile
556
styrene acrylonitrile
557
styrene allyl alcohol
558
styrene butadiene
559
styrene butyl methacrylate
560
styrene ethylene butylene
561
styrene isoprene
562
styrene maleic anhydride
563
styrene maleic anhydride partial methyl ester
564
fibre thermoplastic elastomere
565
fibre turf
566
fibre tussah silk
567
vinyl chloride vinyl acetate
568
vinyl chloride vinyl acetate hydroxypropyl acrylate
569
vinyl chloride vinyl acetate maleic acid
570
vinylidene chloride acrylonitrile
571
vinylidene chloride vinyl chloride
572
fibre viscose
573
fibre viscose dyed
574
fur wild boar
575
wool
576
wool slubbing fine
577
wool slubbing rough
578
wool sheep supersoft
579
fur yak
580
fur yak bleached
581
zein purified
582
cigarette filter
583
polyethylene high density
584
polyethylene low density
585
polyamide 66
586
polycarbonate
587
polyethylene terephthalate
588
polymethyl methacrylate
589
polypropylene
590
polystyrene
591
polyvinylchloride
592
coal
593
coal
594
coal
595
coal
596
coal
597
coal
598
PVA with Kaolin clay
599
Pthalate and propyl alcohol mix
600
polystyrene
601
Polypropylene  with silicate mix
602
Polyethylene
603
Polyethereamide
604
Polyester
605
Polyester
606
polycarbonate
607
Polyethylene
608
papercup_cellulosic
609
polyethylene
610
polu butadiene acrylonitrile
611
Plumbers tape sealing putty
612
polyesterterpthalate
613
poly(ethylene terepthalate)
614
PET
615
polyesterterpthalate
616
PE+silicate+bio
617
PE with silicate inorganic
618
Polyethylene with acryloid and pthalocyanine (blue)
619
PDMS
620
PDMS
621
nitrocellulose
622
Nitrocellulose
623
leaf-plant-
624
cellulose
625
cellulose
626
Cardboard/cellulose
627
Teflon/PTFE
628
Acrylic
629
Polyethylene
630
Cellulose
631
Acrylic
632
Polyether urethane with additives
633
PMMA
634
Teflon/PTFE
635
Styrofoam
636
silicone/PDMS
In [7]:
X = np.asarray(X)
Y = np.asarray(Y)

X_train, X_test, y_train, y_test = train_test_split(X, Y, test_size=0.30, shuffle=True, random_state=0)
 
model = Sequential()
model.add(ConvLSTM2D(filters = 64, kernel_size = (3, 3), return_sequences = False, data_format = "channels_last", input_shape = (1, 100, 400, 1)))
model.add(Dropout(0.2))
model.add(Flatten())
model.add(Dense(128, activation="relu"))
model.add(Dropout(0.3))
model.add(Dense(len(classes), activation = "softmax"))
 
model.summary()
 
opt = keras.optimizers.SGD(lr=0.001)
model.compile(loss='categorical_crossentropy', optimizer=opt, metrics=["accuracy"])
 
earlystop = EarlyStopping(patience=5)
callbacks = [earlystop]
 
history = model.fit(x = X_train, y = y_train, epochs=15, batch_size = 8 , shuffle=True, validation_split=0.20, callbacks=callbacks)
Model: "sequential_1"
_________________________________________________________________
 Layer (type)                Output Shape              Param #   
=================================================================
 conv_lstm2d_1 (ConvLSTM2D)  (None, 98, 398, 64)       150016    
                                                                 
 dropout_2 (Dropout)         (None, 98, 398, 64)       0         
                                                                 
 flatten_1 (Flatten)         (None, 2496256)           0         
                                                                 
 dense_2 (Dense)             (None, 128)               319520896 
                                                                 
 dropout_3 (Dropout)         (None, 128)               0         
                                                                 
 dense_3 (Dense)             (None, 251)               32379     
                                                                 
=================================================================
Total params: 319,703,291
Trainable params: 319,703,291
Non-trainable params: 0
_________________________________________________________________
Epoch 1/15
/usr/local/lib/python3.7/dist-packages/keras/optimizer_v2/gradient_descent.py:102: UserWarning: The `lr` argument is deprecated, use `learning_rate` instead.
  super(SGD, self).__init__(name, **kwargs)
45/45 [==============================] - 10s 175ms/step - loss: 3.7610 - accuracy: 0.4073 - val_loss: 2.9975 - val_accuracy: 0.4719
Epoch 2/15
45/45 [==============================] - 8s 168ms/step - loss: 2.8015 - accuracy: 0.5140 - val_loss: 2.8397 - val_accuracy: 0.5169
Epoch 3/15
45/45 [==============================] - 8s 169ms/step - loss: 2.2699 - accuracy: 0.6545 - val_loss: 2.7217 - val_accuracy: 0.5169
Epoch 4/15
45/45 [==============================] - 8s 169ms/step - loss: 1.7160 - accuracy: 0.7584 - val_loss: 2.6833 - val_accuracy: 0.5506
Epoch 5/15
45/45 [==============================] - 8s 168ms/step - loss: 1.2854 - accuracy: 0.8539 - val_loss: 2.6580 - val_accuracy: 0.5730
Epoch 6/15
45/45 [==============================] - 8s 168ms/step - loss: 0.9787 - accuracy: 0.8904 - val_loss: 2.6630 - val_accuracy: 0.5730
Epoch 7/15
45/45 [==============================] - 8s 168ms/step - loss: 0.7155 - accuracy: 0.9326 - val_loss: 2.6417 - val_accuracy: 0.5618
Epoch 8/15
45/45 [==============================] - 8s 177ms/step - loss: 0.5775 - accuracy: 0.9354 - val_loss: 2.6375 - val_accuracy: 0.5506
Epoch 9/15
45/45 [==============================] - 8s 169ms/step - loss: 0.4808 - accuracy: 0.9438 - val_loss: 2.6341 - val_accuracy: 0.5618
Epoch 10/15
45/45 [==============================] - 8s 169ms/step - loss: 0.3638 - accuracy: 0.9663 - val_loss: 2.6536 - val_accuracy: 0.5730
Epoch 11/15
45/45 [==============================] - 8s 168ms/step - loss: 0.3000 - accuracy: 0.9635 - val_loss: 2.6596 - val_accuracy: 0.5730
Epoch 12/15
45/45 [==============================] - 8s 168ms/step - loss: 0.2456 - accuracy: 0.9747 - val_loss: 2.6466 - val_accuracy: 0.5843
Epoch 13/15
45/45 [==============================] - 8s 168ms/step - loss: 0.2389 - accuracy: 0.9691 - val_loss: 2.6531 - val_accuracy: 0.5843
Epoch 14/15
45/45 [==============================] - 8s 168ms/step - loss: 0.1961 - accuracy: 0.9803 - val_loss: 2.6548 - val_accuracy: 0.5843
In [9]:
# Calling `save('my_model')` creates a SavedModel folder `my_model`.
model.save("my_model")
INFO:tensorflow:Assets written to: my_model/assets